﻿using PersonnelStatistics.BLL;
using PersonnelStatistics.Entities;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 人员统计
{
    public partial class Form3 : Form
    {
        private readonly PersonalService _personalSevice;
        public Form3()
        {
            InitializeComponent();
            _personalSevice = new PersonalService();
        }

        private void Form3_Load(object sender, EventArgs e)
        {
            this.Text = "数据查询修改";
        }

        private void comboBox7_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox7.Text != "")
            {
                textBox10.Enabled = true;
                textBox10.Focus();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Hide();

            this.Close();
        }
        private void button3_Click(object sender, EventArgs e)
        {
            if (comboBox7.Text == "")
            {
                MessageBox.Show("请先输入查询条件和内容！");
                comboBox7.Focus();
            }
            else
            {
                var data = _personalSevice.GetList(textBox10.Text).ToList();

                this.dataGridView1.DataSource = data;

                if (data.Count > 0)
                {
                    MessageBox.Show("找到条件为：【" + comboBox7.Text + "】 内容为：【" + textBox10.Text + "】的数据" + dataGridView1.Rows.Count.ToString() + "条，如需修改操作请选择相应内容！");
                }
                else
                {
                    MessageBox.Show("没有找到内容为：【" + textBox10.Text + "】的数据，请重新输入！");
                    textBox10.Text = "";
                    textBox10.Focus();

                }
            }

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            int rowindex = e.RowIndex;

            string path1 = System.IO.Directory.GetCurrentDirectory();

            //在这里取数。两种取法，
            //一种，从数据库取。通过id重新从数据库查询 。这样。可以刷新数据。因为一般软件 。很可能。你看到 数据在其它地方被别人改了
            //刚好。你修改的时候。再重新从数据库加载一次
            //第二种，就是本地加载。本地加载也不要从UI里面加载。从它的数据源
            //我建议从数据库取。比如写成下面


            //知道了
            var id = (int)dataGridView1.Rows[rowindex].Cells[0].Value;

            var data = _personalSevice.GetSingle(id);
            label20.Text = data.Id.ToString();
            textBox1.Text = data.姓名.ToString();
            comboBox1.Text = data.性别.ToString();
            textBox11.Text = data.年龄.ToString();
            comboBox2.Text = data.学历.ToString();
            comboBox3.Text = data.部门.ToString();
            comboBox4.Text = data.职位.ToString();
            comboBox5.Text = data.婚姻.ToString();
            textBox2.Text = data.工资.ToString();
            comboBox6.Text = data.政治面貌.ToString();
            textBox3.Text = data.身份证.ToString();
            textBox4.Text = data.住址.ToString();
            textBox5.Text = data.电话.ToString();
            textBox8.Text = data.入职时间.ToString();
            textBox9.Text = data.离职时间.ToString();
            textBox6.Text = data.备用联系人.ToString();
            textBox7.Text = data.联系人电话.ToString();
            string imageFlie = (path1 + "\\Image\\" + data.照片.ToString()); //定义图片位置

            this.pictureBox1.ImageLocation = imageFlie; //读取图片显示
            if (textBox1.Text != "") { button2.Enabled = true; } else { button2.Enabled = false; }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            DisabledEdit(true);

        }

        private void groupBox2_Enter(object sender, EventArgs e)
        {

        }

        private void button4_Click(object sender, EventArgs e)
        {

            var id = int.Parse(label20.Text);
            var personal = new Personal();
            personal.Id = id;
            personal.姓名 = this.textBox1.Text;
            personal.性别 = this.comboBox1.Text;
            personal.年龄 = Convert.ToInt16(this.textBox11.Text);
            personal.学历 = this.comboBox2.Text;
            personal.部门 = this.comboBox3.Text;
            personal.职位 = this.comboBox4.Text;
            personal.工资 = this.textBox2.Text;
            personal.政治面貌 = this.comboBox6.Text;
            personal.婚姻 = this.comboBox5.Text;
            personal.身份证 = this.textBox3.Text;
            personal.住址 = this.textBox4.Text;
            personal.电话 = this.textBox5.Text;
            personal.入职时间 = this.textBox8.Text;
            personal.离职时间 = this.textBox9.Text;
            personal.备用联系人 = this.textBox6.Text;
            personal.联系人电话 = this.textBox7.Text;
            _personalSevice.AddOrUpdate(personal);

            string name = textBox1.Text;
            MessageBox.Show("修改员工ID：【" + label20.Text + "】 姓名：" + name.Trim() + "的数据成功！");
            DisabledEdit(false);

        }

        private void DisabledEdit(bool a)
        {
            textBox1.Enabled = a;
            textBox2.Enabled = a;
            textBox3.Enabled = a;
            textBox4.Enabled = a;
            textBox5.Enabled = a;
            textBox6.Enabled = a;
            textBox7.Enabled = a;
            textBox8.Enabled = a;
            textBox9.Enabled = a;
            textBox11.Enabled = a;
            comboBox1.Enabled = a;
            comboBox2.Enabled = a;
            comboBox3.Enabled = a;
            comboBox4.Enabled = a;
            comboBox5.Enabled = a;
            comboBox6.Enabled = a;
            button4.Enabled = a;
            button2.Enabled = !a;
            button4.Enabled = a;

        }


    }
}
